<template>
	<view class="">
		<view class="p-t-20 p-l-20">
			<view class="lg" style="color: #999;">
				当前定位城市
			</view>
			<view class="city m-t-20 primary bg-white" @click="againLoca">
				{{!!cityNow?cityNow:'定位失败,点击重试'}}
			</view>
		</view>
		<u-index-list :scrollTop="scrollTop" :sticky="flase" @select="select" active-color="#333">
			<view v-for="(item, index) in indexList" :key="index">
				<u-index-anchor :index="item.key" />
				<view class="list-cell" v-for="item2 in item.child" :key="item2.id" @click="chooseAddress(item2.name,item2.p_name)">
					{{item2.name}}
				</view>
			</view>
		</u-index-list>
	</view>
</template>

<script>
	import {getCityList} from '@/api/car'
	import GDMap  from '@/js_sdk/amap-wx/amap-wx.130'	
	export default {
		data() {
			return {
				scrollTop: 0,
				indexList: [],
				loadShow:false,
				cityNow:null,
				mapgd:null
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
			// console.log(e);
		},
		async onLoad(option) {
			console.log(option);
			if(option.unAddress==1){
				this.$toast({
					title:'未获取到定位，请手动选择地址或者重新定位'
				})
			}
			
			this.mapgd =  new GDMap.AMapWX({key:'364c1096e6b21c6e94ffaf5cc02e4d08'});
			uni.showLoading({
				title:'加载中'
			})
			
			
			await this.getCityListFun();
			this.getUserLocation();	
		},
		beforeDestroy(){
			
			// if(this.cityNow!=null){
			// 	let pages = getCurrentPages(); // 当前页面
			// 	let beforePage = pages[pages.length - 2]; // 上一页
			// 	console.log(beforePage)
			// 	beforePage.$vm.cityNow = this.cityNow;
			// }
		},
		methods:{
			againLoca(){
				if(this.cityNow==null){
					this.getUserLocation();
					
				}
				if(this.cityNow!=null){
					this.chooseAddress(this.cityNow)
				}
				
				
			},
			chooseAddress(name,p_name){
				let obj  = {
					province:p_name,
					city: name,
				}
				let newObj = this.$u.deepClone(obj)
				
				this.$store.commit('setSwiperAddress',newObj)
				
				console.log(name)
				let pages = getCurrentPages(); // 当前页面
				let beforePage = pages[pages.length - 2]; // 上一页
				console.log(beforePage)
				
				beforePage.$vm.changeCity(name);
				uni.navigateBack({
					
				})
			},
			getCityListFun(){
				getCityList().then(res=>{
					console.log(res)
					this.indexList = res.data;
					uni.hideLoading();
					this.loadShow = true;
					
				})
			},
			select(index){
				console.log(index);
			},
			getUserLocation(){
				var _this = this;
				uni.getLocation({
					type: 'gcj02',
					success:res=>{
						var latitude = res.latitude
						var longitude = res.longitude
						this.mapgd.getRegeo({
							 location:`${longitude},${latitude}`,
							 success:res=>{
								 console.log(res);	
								this.cityNow  = res[0].regeocodeData.addressComponent.city;
							 },
							 fail:err=>{
								 console.log(err);
							 }
						})
					},
					fail:err=>{
						console.log(err)
						uni.showModal({
							title:'提示',
							content:'是否请授权您的位置信息',
							success:res=>{
								if (res.confirm) {
									uni.openSetting({
										success(res) {
											console.log(res.authSetting)
											if (res.authSetting['scope.userLocation']) {
												// _this.getUserLocation();
												_this.getUserLocation()
											}
										}
									})
								}
							}
						})
						
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.city{
		border-radius: 10upx;
		border: 1upx solid #a0a0a0;
		display: inline-block;
		padding: 10upx 20upx;
	}
	.list-cell {
		display: flex;
		box-sizing: border-box;
		width: 100%;
		padding: 10px 24rpx;
		overflow: hidden;
		color: #323233;
		font-size: 14px;
		line-height: 24px;
		background-color: #fff;
	}
</style>